<?php
/**
 * Created by PhpStorm.
 * User: 10838
 * Date: 2017/11/15
 * Time: 9:47
 */
namespace app\wechat\controller;

use app\admin\model\OpinionModel;
use app\admin\model\UserModel;
use app\common\model\IntegralModel;
use app\common\model\ProjectModel;
use app\common\model\ReplyModel;
use think\Controller;
use think\Loader;
use wechat\wechatCallbackapiTest;

class Wechat extends Controller
{
    public $appid  = 'wxc7f82feeff1f94fb';
    public $secret = '94abecfdad50a8609827c1c27adba840';
    public function __construct()
    {
        Loader::import('wechat.wechatCallbackapiTest');
    }







    //自定义菜单
    public function meun()
    {


        $wechbat_api = new wechatCallbackapiTest();

     $access_token = $wechbat_api->get_token();

        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
       /* $data = '{
             "button":[
              {
                   "name":"优家舒适圈",
                   "sub_button":[
                   {
                       "type":"view",
                       "name":"优家舒适圈",
                       "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc7f82feeff1f94fb&redirect_uri=http%3a%2f%2fmeijing.scdxtc.com&response_type=code&scope=snsapi_userinfo&state=diyadaikuan#wechat_redirect"
                    }]
               }
               ]
         }';*/
        $data = '{
             "button":[
              {
                   "name":"俱乐部",
                    "type":"view",
                    "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc7f82feeff1f94fb&redirect_uri=http%3a%2f%2fmeijing.scdxtc.com&response_type=code&scope=snsapi_userinfo&state=diyadaikuan#wechat_redirect"

               }
               ],
               "button":[
              {
                   "name":"使用帮助",
                    "type":"view",
                    "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc7f82feeff1f94fb&redirect_uri=http%3a%2f%2fmeijing.scdxtc.com%2findex%2fhelp%2fhelp.html&response_type=code&scope=snsapi_userinfo&state=diyadaikuan#wechat_redirect"

               }
               ],
               "button":[
              {
                   "name":"联系我们",
                    "type":"view",
                    "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc7f82feeff1f94fb&redirect_uri=http%3a%2f%2fmeijing.scdxtc.com%2findex%2fhelp%2findex.html&response_type=code&scope=snsapi_userinfo&state=diyadaikuan#wechat_redirect"

               }
               ]

         }';
        $re = $this->curl_wechat($url, $data);
        var_dump($re);
    }

    public function curl_wechat($url, $data)
    {

        //初始化
        $ch = curl_init();
        //设置请求地址
        curl_setopt($ch, CURLOPT_URL, $url);
        //捕获内容但不输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //模拟post发送
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        //执行
        $re = curl_exec($ch);

        //判断是否发送成功
        if ($re == false) {
            echo curl_error($ch);
        } else {
            return $re;
        }
        //关闭
        curl_close($ch);
    }

//    public function msg_send()
//    {
//        $this->project();
//        $this->integral();
//        $this->reply();
//
//
//    }
//
//    //获取项目状态变化
//    public function project()
//    {
//        $re = ProjectModel::where('is_send', '0')->select();
//        if ($re) {
//            foreach ($re as $k => $v) {
//                ProjectModel::where('address', $v->address)->update([
//                    'is_send' => '1'
//                ]);
//                //查询用户
//                $data = [];
//                $data['title'] = '项目状态';
//                $data['content'] = $v->address . '<br/>项目状态:' . $v->prostatus . "<br/>积分状态:" . $v->linkstatus;
//                $data['type'] = "integral";
//                $data['url'] = "http://" . $_SERVER['SERVER_NAME'] . "/index/message/project_details/id/{$v->id}/type/message.html";
//                $user = UserModel::get($v->user_id);
//                $this->send_api($data, $user['openid']);
//            }
//        } else {
//            echo '没有未发送的消息';
//        }
//    }
//
//    //积分
//    public function integral()
//    {
//        $re = IntegralModel::where('is_send', '0')->select();
//        if ($re) {
//            foreach ($re as $k => $v) {
//                IntegralModel::where('address', $v->address)->update([
//                    'is_send' => '1'
//                ]);
//                //查询用户
//                $user = UserModel::get($v->user_id);
//                $data = [];
//                $data['title'] = '积分变化';
//                $data['content'] = $v->address . '<br/>' . $v->point;
//                $data['type'] = "integral";
//                //  http://meijing.scdxtc.com/index/message/project_details/id/3/type/integral.html
//                $data['url'] = "http://" . $_SERVER['SERVER_NAME'] . "/index/message/project_details/id/{$v->id}/type/integral.html";
//
//                $this->send_api($data, $user['openid']);
//            }
//
//        } else {
//            echo '没有未发送的消息';
//        }
//    }

//    //消息
//    public function reply()
//    {
//        $reply = ReplyModel::where('is_send', '0')->select();
//        if ($reply) {
//            foreach ($reply as $k => $v) {
//                ReplyModel::where('id', $v->id)->update([
//                    'is_send' => '1'
//                ]);
//                $re = OpinionModel::get($v->opinion_id);
//                if ($re) {
//                    $user = UserModel::get($re->user_id);
//                    if ($user) {
//                        $data = [];
//                        //项目
//                        if ($re->type == '1') {
//                            $title_names = feedback_type_1();
//                            $data['title'] = "项目消息回复";
//                            $data['address'] = $re->project_id;
//                            $data['title_name'] = $title_names[$re->title];
//                            $data['content'] = "";
//                            $data['url'] = "http://" . $_SERVER['SERVER_NAME'] . "/index/message/project_details/id/{$v->id}/type/message.html";
//                        } elseif ($re->type == '2') {
//                            //普通
//                            $title_names = feedback_type_2();
//                            $data['title'] = "普通消息回复";
//                            $data['title_name'] = $title_names[$re->title];
//                            $data['content'] = "";
//                            $data['url'] = "http://" . $_SERVER['SERVER_NAME'] . "/index/message/project_details/id/{$v->id}/type/message.html";
//                        }
//                        $this->send_api($data, $user['openid']);
//                    }
//                }
//
//            }
//        }
//    }
//
//    //消息推送
//    public function send_api($data, $openid)
//    {
//        $url_text = urlencode($data['url']);
//        // $url_text = "http://".$_SERVER['SERVER_NAME'].'/index/message/project_details/id/1/type/message.html';
//        $access_token = $this->get_access_token();
//        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";
//        $text = "<a herf='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx703acc3701dcc383&redirect_uri={$url_text }&response_type=code&scope=snsapi_userinfo&state=diyadaikuan#wechat_redirect' >尊敬的:\n贷管网祝您生日快乐，身体健康！</a>" . $data['title'] . "<br/>" . $data['content'];
//        $date = '{
//                            "touser":"' . $openid . '",
//                            "msgtype":"text",
//                            "text":
//                            {
//                                 "content":"' . $text . '"
//                            }
//                        }';
//
//        //$date = json_encode($date,true);
//        return $this->curl_date($url, $date);
//    }

    //curl
    protected function curl_date($url, $date)
    {
        //初始化
        $ch = curl_init();
        //设置请求地址
        curl_setopt($ch, CURLOPT_URL, $url);
        //捕获内容但不输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //模拟post发送
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $date);

        //执行
        $re = curl_exec($ch);
        //判断是否发送成功
        if ($re == false) {
            echo curl_error($ch);
        } else {
            echo $re;
        }
        //关闭
        curl_close($ch);
    }

    //获取assess_token
    public function get_access_token()
    {
        /*$appid = 'wx703acc3701dcc383';
        $secret = '1c9157d07bcb15c437b5986b7b52eba4';*/
        $appid = 'wxc7f82feeff1f94fb';
        $secret = '94abecfdad50a8609827c1c27adba840';
        $get_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        $date = file_get_contents($get_url);
        $date = json_decode($date, true);

        return $date['access_token'];

    }


}